package com.wxtwxt.ruralmall.ware.service.impl;

import com.wxtwxt.ruralmall.common.constant.WareConstant;
import com.wxtwxt.ruralmall.ware.entity.PurchaseDetailEntity;
import com.wxtwxt.ruralmall.ware.service.PurchaseDetailService;
import com.wxtwxt.ruralmall.ware.service.WareSkuService;
import com.wxtwxt.ruralmall.ware.vo.MergeVo;
import com.wxtwxt.ruralmall.ware.vo.PurchaseDetailDoneVo;
import com.wxtwxt.ruralmall.ware.vo.PurchaseDoneVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxtwxt.ruralmall.common.utils.PageUtils;
import com.wxtwxt.ruralmall.common.utils.Query;

import com.wxtwxt.ruralmall.ware.dao.PurchaseDao;
import com.wxtwxt.ruralmall.ware.entity.PurchaseEntity;
import com.wxtwxt.ruralmall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Resource
    private PurchaseDetailService purchaseDetailService;
    @Resource
    private WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getUnReceiveList(Map<String, Object> params) {
        QueryWrapper<PurchaseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("status", 0, 1);
        IPage<PurchaseEntity> page = this.page(new Query<PurchaseEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public void merge(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();
        if(purchaseId == null) {
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }
        List<Long> items = mergeVo.getItems();
        Long finalPurchaseId = purchaseId;
        List<Long> newItems = items.stream().filter(item -> {
            PurchaseDetailEntity purchaseDetailEntity = purchaseDetailService.getById(item);
            Integer status = purchaseDetailEntity.getStatus();
            return WareConstant.PurchaseDetailStatusEnum.CREATED.getCode().equals(status) || WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode().equals(status);
        }).collect(Collectors.toList());
        List<PurchaseDetailEntity> purchaseDetailEntities = newItems.stream().filter(item -> {
            PurchaseEntity purchaseEntity = this.getById(finalPurchaseId);
            Integer status = purchaseEntity.getStatus();
            return WareConstant.PurchaseStatusEnum.CREATED.getCode().equals(status) || WareConstant.PurchaseStatusEnum.ASSIGNED.getCode().equals(status);
        }).map(item -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(item);
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(purchaseDetailEntities);
        if(purchaseDetailEntities.size() > 0) {
            PurchaseEntity purchase = new PurchaseEntity();
            purchase.setId(finalPurchaseId);
            purchase.setUpdateTime(new Date());
            this.updateById(purchase);
        }
    }

    @Override
    public void purchaseReceived(List<Long> purchaseIds) {
        // 判断选中的采购单是否可以被领取
        List<PurchaseEntity> collect = purchaseIds.stream().map(this::getById).filter(purchaseEntity -> {
            return WareConstant.PurchaseStatusEnum.CREATED.getCode().equals(purchaseEntity.getStatus()) || WareConstant.PurchaseStatusEnum.ASSIGNED.getCode().equals(purchaseEntity.getStatus());
            // 修改采购单状态
        }).map(purchaseEntity -> {
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.RECEIVED.getCode());
            purchaseEntity.setUpdateTime(new Date());
            return purchaseEntity;
        }).collect(Collectors.toList());
        this.updateBatchById(collect);
        collect.forEach(purchaseEntity -> {
            List<PurchaseDetailEntity> purchaseDetailEntities = purchaseDetailService.queryDetailByPurchaseId(purchaseEntity.getId());
            List<PurchaseDetailEntity> detailEntities = purchaseDetailEntities.stream().map(item -> {
                PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                purchaseDetailEntity.setId(item.getId());
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
                return purchaseDetailEntity;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(detailEntities);
        });
    }

    @Transactional
    @Override
    public void purchaseDone(PurchaseDoneVo purchaseDoneVo) {
        List<PurchaseDetailDoneVo> doneVoList = purchaseDoneVo.getPurchaseDetailDoneVoList();
        Boolean flag = true;
        List<PurchaseDetailEntity> updateList = new ArrayList<>();
        for (PurchaseDetailDoneVo doneVo : doneVoList) {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            if(WareConstant.PurchaseDetailStatusEnum.ERROR.getCode().equals(doneVo.getStatus())) {
                flag = false;
                detailEntity.setStatus(doneVo.getStatus());
            }else {
                detailEntity.setStatus(doneVo.getStatus());
                // 新增或更新库存
                PurchaseDetailEntity entity = purchaseDetailService.getById(doneVo.getItemId());
                wareSkuService.addOrUpdateStock(entity.getSkuId(), entity.getWareId(), entity.getSkuNum());
            }
            detailEntity.setId(doneVo.getItemId());
            updateList.add(detailEntity);
        }
        purchaseDetailService.updateBatchById(updateList);
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseDoneVo.getId());
        if(flag) {
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.FINISH.getCode());
        }else {
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.ERROR.getCode());
        }
        this.updateById(purchaseEntity);
    }

}